package com.databing.demo.ama

import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.databing.demo.R
import com.databing.demo.ama.util.AMapLocation
import com.databing.demo.annotation.ClazzInfo
import com.databing.demo.base.TitleActivity
import com.databing.demo.config.TAG

private const val PERMISSON_REQUESTCODE = 0


/**
 * 需要进行检测的权限数组
 */
private val needPermissions = arrayOf(
    Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.READ_PHONE_STATE
)


@ClazzInfo(3, 0, "第三章", "高地图的demo")
class AMapActivity : TitleActivity() {
    private var url:String=""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_a_map)
    }

    /**
     * 检查是否拥有指定的所有权限
     */
    private fun checkPermissionAllGranted(permissions: Array<String>): Boolean {
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(
                    this,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                // 只要有一个权"权限"+permission+"没有授权");
                return false
            }
        }
        return true
    }

    fun onLocation(view: View) {
        if (checkPermissionAllGranted(needPermissions)) {
            AMapLocation.startLocation()
        }
        ActivityCompat.requestPermissions(
            this,
            needPermissions, PERMISSON_REQUESTCODE
        )
    }


    /**
     * 点位分发链接
     */
    private fun setUrl() {
        url = ""
        url = url.replace(" ", "")
        val code ="123"
            val parameters = Uri.parse(url).queryParameterNames
            if (parameters == null || parameters.isEmpty()) {
                url += "?vrStr=$code"
            } else url += "&vrStr=$code"
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == PERMISSON_REQUESTCODE) {
            val isAllGranted = checkPermissionAllGranted(permissions)
            if (isAllGranted) {
                AMapLocation.startLocation()
            } else {
                Log.e(TAG, "没有权限")
            }
        }
    }
}
